package alfred.proctice.com.recycleviewother;

import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

import alfred.proctice.com.recycleview.R;

/**
 * Created by Alfred on 2018/3/14.
 */

public class HeaderAndFooterWrapper extends RecyclerView.Adapter {
    private static final int BASE_ITEM_TYPE_HEADER = 100000;
    private static final int BASE_ITEM_TYPE_FOOTER = 200000;

    private SparseArrayCompat<View> mHeaderViews = new SparseArrayCompat<>();
    private SparseArrayCompat<View> mFootViews = new SparseArrayCompat<>();

    private RecyclerView.Adapter mInnerAdapter;

    public HeaderAndFooterWrapper(RecyclerView.Adapter adapter)
    {
        mInnerAdapter = adapter;
    }

    private boolean isHeaderViewPos(int position)
    {
        return position < getHeadersCount();
    }

    private boolean isFooterViewPos(int position)
    {
        return position >= getHeadersCount() + getRealItemCount();
    }

    private int getRealItemCount() {
        return mInnerAdapter.getItemCount();
    }

    public void addHeaderView(View view)
    {
        mHeaderViews.put(mHeaderViews.size() + BASE_ITEM_TYPE_HEADER, view);
    }

    public void addFootView(View view)
    {
        mFootViews.put(mFootViews.size() + BASE_ITEM_TYPE_FOOTER, view);
    }

    public int getHeadersCount()
    {
        return mHeaderViews.size();
    }

    public int getFootersCount()
    {
        return mFootViews.size();
    }

    public View getHeaderView(){
        return mHeaderViews.get(0);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHeaderViews.get(viewType)!=null){
            return new ViewHolder(mHeaderViews.get(viewType));
        }else if(mFootViews.get(viewType) != null){
            return new ViewHolder(mFootViews.get(viewType));
        }else{
            return mInnerAdapter.onCreateViewHolder(parent,viewType);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(isHeaderViewPos(position)){
            return ;
        }else if(isFooterViewPos(position)){
            return ;
        }else {
            mInnerAdapter.onBindViewHolder(holder,position-getHeadersCount());
        }
    }

    @Override
    public int getItemCount() {
        return getHeadersCount()+getFootersCount()+getRealItemCount();
    }

    @Override
    public int getItemViewType(int position) {
        if(isHeaderViewPos(position)){
            return mHeaderViews.keyAt(position);
        }else if(isFooterViewPos(position)){
            return mFootViews.keyAt(position-getHeadersCount()-getRealItemCount());
        }else {
            return mInnerAdapter.getItemViewType(position);
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView imageView;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.item_topfresh_img);
        }
    }
}
